import { Col } from "../../../../core/data_ext";
import Character from "./character";
import CharacterCtrl from "./character_ctrl";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CharacterCtrlKeyBoard extends CharacterCtrl {
    @property
    keyUp = "";
    @property
    keyDown = "";
    @property
    keyLeft = "";
    @property
    keyRight = "";
    @property
    keyGrabDrop = "";
    @property
    keyUse = "";
    @property
    keyJump = "";

    keyBinding: Col<cc.macro.KEY> = {};
    onDetach(char: Character): void {
        this.reset();
    }
    reset() {
        this.side = 0;
    }
    protected onLoad(): void {
        this.keyBinding["keyUp"] = cc.macro.KEY[this.keyUp];
        this.keyBinding["keyDown"] = cc.macro.KEY[this.keyDown];
        this.keyBinding["keyLeft"] = cc.macro.KEY[this.keyLeft];
        this.keyBinding["keyRight"] = cc.macro.KEY[this.keyRight];
        this.keyBinding["keyGrabDrop"] = cc.macro.KEY[this.keyGrabDrop];
        this.keyBinding["keyUse"] = cc.macro.KEY[this.keyUse];
        this.keyBinding["keyJump"] = cc.macro.KEY[this.keyJump];
    }

    protected onCtrlEnable(): void {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
        this.side = 0;
    }
    protected onCtrlDisable(): void {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
        this.side = 0;
    }

    protected onEnable(): void {
        if (this.ctrlEnabled) {
            this.onCtrlEnable();
        }
    }
    protected onDisable(): void {
        if (this.ctrlEnabled) {
            this.onCtrlDisable();
        }
    }
    onKeyDown(evt: cc.Event.EventKeyboard) {
        if (!cc.isValid(this.char)) {
            return;
        }

        switch (evt.keyCode) {
            case this.keyBinding["keyJump"]:
                this.jump();
                break;
            case this.keyBinding["keyGrabDrop"]:
                this.grabDrop();
                break;
            case this.keyBinding["keyUse"]:
                this.use();
                break;
            case this.keyBinding["keyUp"]:
                this.side |= 0b1;
                break;
            case this.keyBinding["keyRight"]:
                this.side |= 0b10;
                break;
            case this.keyBinding["keyDown"]:
                this.side |= 0b100;
                break;
            case this.keyBinding["keyLeft"]:
                this.side |= 0b1000;
                break;
        }
    }
    onKeyUp(evt: cc.Event.EventKeyboard) {
        if (!cc.isValid(this.char)) {
            return;
        }
        switch (evt.keyCode) {
            case this.keyBinding["keyUse"]:
                this.unuse();
                break;
            case this.keyBinding["keyUp"]:
                this.side -= this.side & 0b1;
                break;
            case this.keyBinding["keyRight"]:
                this.side -= this.side & 0b10;
                break;
            case this.keyBinding["keyDown"]:
                this.side -= this.side & 0b100;
                break;
            case this.keyBinding["keyLeft"]:
                this.side -= this.side & 0b1000;
                break;
        }
    }
};